<?php

/**
 * Project:     Axe: the PHP compiling template engine
 * File:        Axe.class.php
 * Git:         https://git.oschina.net/koodo/Axe.git
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * For questions, help, comments, discussion, etc., please join the
 * Smarty mailing list. Send a blank e-mail to
 * smarty-discussion-subscribe@googlegroups.com
 *
 * @link http://www.iwshop.cn/
 * @copyright 2014 ycchen
 * @author Ycchen <koodo@qq.com>
 * @version 1.0.0
 */
// cache
require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'Axe.cache.filebase.php';
// compiler
require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'Axe.compiler.class.php';
// exception
require_once dirname(__FILE__) . DIRECTORY_SEPARATOR . 'Axe.exception.class.php';

class Axe {

    /**
     * config array of Axe
     * @var array 
     */
    public $config;

    /**
     * class root of Axe
     * @var path 
     */
    private $class_root;

    /**
     * real root of App
     * @var path 
     */
    public $real_path;

    /**
     * 参数栈
     * @var mixed 
     */
    private $aStack;

    /**
     * 模板编译类
     * @var type 
     */
    private $compiler;

    /**
     * 模板缓存类
     * @var type 
     */
    private $cacheobj;

    /**
     * 模板文件路径
     * @var path 
     */
    public $tplfile;

    /**
     * __construct
     * @param type $overRide
     */
    public function __construct($overRide = false) {
        /**
         * Axe 被动调用目录
         */
        $this->real_path = realpath('.') . DIRECTORY_SEPARATOR;
        /**
         * Axe class 文件目录
         */
        $this->class_root = dirname(__FILE__) . DIRECTORY_SEPARATOR;
        /**
         * 加载配置文件
         */
        $this->config = require $this->class_root . 'Axe.config.inc.php';
        // config 覆盖
        if ($overRide) {
            foreach ($overRide as $k => $v) {
                $this->config[$k] = $v;
            }
        }
    }

    /**
     * 
     * @param type $size
     * @return type
     */
    private function bytes_convert($size) {
        $unit = array('b', 'kb', 'mb', 'gb', 'tb', 'pb');
        return round($size / pow(1024, ($i = floor(log($size, 1024)))), 2) . $unit[$i];
    }

    /**
     * 参数注册
     * 
     * @param type $key
     * @param type $value
     * @return type
     */
    public function assign($key, $value) {
        return $key && $key != "" && $this->aStack[$key] = $value;
    }

    /**
     * 
     * @param type $tpl
     * @param type $cacheId
     */
    public function show($tpl, $cacheId = false) {
        // 加载模板文件
        $this->tplfile = $this->real_path . $this->config['tpl_dir'] . DIRECTORY_SEPARATOR . $tpl . $this->config['tpl_suffix'];
        if (!is_file($this->tplfile)) {
            // file not found !
            return new AxeException('Tpl File Not Found ! Path:' . $this->tplfile);
        } else {
            // modify config -> cacheId[false]
            $this->config['cacheId'] = $cacheId;
            $this->cacheobj = new AxeCache();
            $this->cacheobj->axe = $this;
            // 缓存检查
            if ($this->cacheobj->isCached($this->tplfile, $cacheId)) {
                // 已缓存，未过期，直接输出缓存内容
                $this->cacheobj->echoCache();
            } else {
                $this->cacheobj->start();
                // init compiler
                $this->compiler = new AxeCompiler();
                // link Axe object pointer
                $this->compiler->axe = $this;
                // checking
                $comFile = $this->compiler->compile();
                // directly render[aStack]
                if (is_file($comFile)) {
                    require $comFile;
                    $this->cacheobj->end();
                } else {
                    return new AxeException('Tpl File Can\'t be Compile Or Be Save ! Path:' . $this->tplfile);
                }
            }
            unset($this);
        }
    }

    /**
     * 设置模板目录
     * @param type $dir
     */
    public function setTplDir($dir) {
        return $this->config['tpl_dir'] = $dir;
    }

    public function get_mem_usage() {
        return $this->bytes_convert(memory_get_usage());
    }

    public function debug() {
        var_dump($this->aStack);
    }

}
